<?php

namespace app\admin\validate;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Validate;

class Institution extends Validate
{
    protected $rule = [
        'name' => 'require',
        'category' => 'require|checkCategory',
        'area' => 'require|checkCategory',
        'status' => 'require',
        'id' => 'require',
    ];

    protected $scene=[
        'create'=>['name','category','area','status'],
        'update'=>['name','category','area','status','id'],
    ];

    protected $message = [
        'name' => '机构名称不能为空',
        'category' => '机构类别不能为空',
        'area' => '所属地区不能为空',
        'status' => '状态不能为空',
        'id' => 'ID不能为空',
    ];
    /**
     * @param $value
     * @param $rule
     * @param array $data
     * @return string|true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function checkCategory($value, $rule, array $data = [])
    {
        $category=(new \app\common\business\Category())->categoryDictionaries();
        if(!isset($category[$value])){
            return '类别不存在';
        }
        return true;
    }
}
